package baseproject.com.mybaseproject.ui.base

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import baseproject.com.mybaseproject.mvp.contract.IContract
import baseproject.com.mybaseproject.utils.ToastUtils
import com.gyf.barlibrary.ImmersionBar
import com.tbruyelle.rxpermissions2.RxPermissions
import com.xiasuhuei321.loadingdialog.view.LoadingDialog

abstract class BaseFragment : Fragment(), IContract.IBaseView {

    open lateinit var rxPermissions: RxPermissions
    //沉浸式
    open lateinit var mImmersionBar : ImmersionBar

    open  var ld : LoadingDialog?= null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rxPermissions = RxPermissions(this)
        return getLayoutId()?.let { inflater.inflate(it, null) }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (isImmersionBarEnabled()){
            initImmersionBar()
        }
    }

    /**
     * 初始化沉浸式
     */
    open fun initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this)
        mImmersionBar.keyboardEnable(true)
//                .navigationBarWithKitkatEnable(false)
                .statusBarDarkFont(true, 0.2f)
                .init()
    }

    /**
     * 是否在Fragment使用沉浸式  默认使用
     *
     * @return the boolean
     */
    open fun isImmersionBarEnabled(): Boolean {
        return true
    }
    override fun showToast(msg: String?) {
        if (msg != null) {
            ToastUtils.showToast(msg)
        }
    }
    override fun showToast(msg: Int) {
        ToastUtils.showToast(msg)
    }

    /**
     * 显示加载条
     */
    override fun showProgress(msg: String) {
        ld = LoadingDialog(activity)
        ld?.setInterceptBack(false)
                ?.setLoadingText(msg)
                ?.show()
    }

    /**
     * 隐藏加载条
     */
    override fun hideProgress() {
        ld?.close()
    }

    /**
     * 获取子布局
     */
    abstract fun getLayoutId(): Int?

    override fun onDestroy() {
        super.onDestroy()
        if (mImmersionBar != null)
            mImmersionBar.destroy()
    }

}